#include <string.h>

#if (!defined (__SDCC_mcs51))

/* Generic routine first */
size_t strlen( const char * str ) {
  register int i = 0 ;
  while( *str++ )
  { i++ ; }
  return i;
}

#else

#if defined(__SDCC)
#include <sdcc-lib.h>
#endif

/* Assembler version for mcs51 */
size_t strlen( const char * str ) __naked {
  str;     /* hush the compiler */

  __asm
  ;
  dptr holds pointer
  ;
  b holds pointer memspace
  ;

  ;
  char *ptr = str:
  mov     r2, dpl
  mov     r3, dph
  ;

  ;
  while( *ptr ) {
    ptr++;
  }
L00101$:
  lcall   __gptrget
  jz      L00102$
  inc     dptr
  sjmp    L00101$
  ;

L00102$:
  ;
  return ptr - str;
  clr     c
  mov     a, dpl
  subb    a, r2
  mov     dpl, a
  ;
  mov     a, dph
  subb    a, r3
  mov     dph, a
  ;
  _RETURN
  __endasm;
}

#endif
